package cn.net.cfss.fgbp.provider.console.biz;

import cn.net.cfss.fgbp.api.console.constant.DeleteFlagEnum;
import cn.net.cfss.fgbp.api.console.dto.SysRoleDto;
import cn.net.cfss.fgbp.api.console.vo.SysRoleVo;
import cn.net.cfss.fgbp.base.common.biz.BaseBiz;
import cn.net.cfss.fgbp.provider.console.dao.SysRoleDtoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author oygq
 * @title: RoleDtoBiz
 * @description: TODO
 * @date 2019/4/2 11:20
 */
@Slf4j
@Service
public class SysRoleDtoBiz extends BaseBiz<SysRoleDtoMapper, SysRoleDto> {

    @Autowired
    SysRedisCacheBiz redisCacheBiz;

    @Transactional
    public void deleteBatch(List<Long> roleIds){
        for(Long id : roleIds){
            //deleteById(Long.valueOf(id));
            SysRoleVo vo = new SysRoleVo();
            vo.setId(id);
            vo.setUpdateTime(new Date());
            vo.setDeleteFlag(DeleteFlagEnum.DEL);
            updateSelectiveById(vo);
        }
    }

    public List<SysRoleDto> queryList(SysRoleDto dto, boolean isSuperAdmin){
        return mapper.queryList(dto, isSuperAdmin);
    }

    public boolean check(String roleCode, Long id) {
        if (id != null) {
            SysRoleDto dto = selectById(id);
            if (roleCode.equals(dto.getRoleCode())) {
                return true;
            }
        }
        if (mapper.check(roleCode) > 0) {
            return false;
        }
        return true;
    }

    /**
     * 删除角色缓存
     * @param roleIds
     */
    public void deleteRoleAuthCache(List<Long> roleIds){
        for(Long roleId : roleIds){
            if(null == roleId){
                continue;
            }
            try {
                redisCacheBiz.roleDelete(roleId);
            } catch (Exception e) {
                //e.printStackTrace();
                log.error("角色权限缓存删除错误:{},角色id:{}", e.getMessage(), roleId);
            }
        }
    }

    public void deleteRoleAuthCache(Long roleId){
        if(null == roleId){
            return;
        }
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(roleId);
        deleteRoleAuthCache(roleIds);
    }

}
